package com.pan.hisystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pan.hisystem.mapper.PrescriptionDrugsMapper;
import com.pan.hisystem.pojo.*;
import com.pan.hisystem.mapper.DrugMapper;
import com.pan.hisystem.service.IDrugService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pan.hisystem.service.IPrescriptionDrugsService;
import com.pan.hisystem.service.IPrescriptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author qingyang
 * @since 2022-03-18
 */
@Service
public class DrugServiceImpl extends ServiceImpl<DrugMapper, Drug> implements IDrugService {

    @Autowired
    private DrugMapper drugMapper;
    @Autowired
    private IPrescriptionDrugsService prescriptionDrugsService;
    @Autowired
    private IPrescriptionService prescriptionService;
    @Autowired
    private PrescriptionDrugsMapper prescriptionDrugsMapper;
    @Autowired
    private IDrugService drugService;


    /**
     * 取药
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public RespBean updateDrugs(Integer[] ids) {

        //药品数量更新

        List<PrescriptionDrugs> prescriptionDrugsList=prescriptionDrugsMapper.selectBatchIds(Arrays.asList(ids.clone()));//拷贝数组，再进行查询
        List<Drug> drugList=drugMapper.selectList(new QueryWrapper<Drug>().eq("enabled",true));
        List<Drug> drugList1=new ArrayList<>();
        for (PrescriptionDrugs prescriptionDrug : prescriptionDrugsList) {
            for (Drug drug : drugList) {
                if(prescriptionDrug.getDrugId().equals(drug.getId())){
                    if(drug.getStorageQuantity()-prescriptionDrug.getNum()>=0){
                        drug.setStorageQuantity(drug.getStorageQuantity()-prescriptionDrug.getNum());
                        drugList1.add(drug);

                    }else{
                        return RespBean.error("取药失败,"+drug.getName()+"数量不足!");
                    }
                }
            }
        }
        if(drugService.updateBatchById(drugList1)){
            int k=prescriptionDrugsService.updateDrugs(ids);
            if(k==ids.length){
                //获取药品对应处方单，修改处方状态为完成取药
                PrescriptionDrugs prescriptionDrugs=prescriptionDrugsService.getOne(new QueryWrapper<PrescriptionDrugs>()
                        .eq("id",ids[0]));
                Prescription prescription=prescriptionService.getById(prescriptionDrugs.getPreId());
                prescription.setTakeStatus(true);
                prescriptionService.updateById(prescription);

                return RespBean.success("取药成功!");
            }
        }

        return RespBean.error("取药失败!");
    }

    /**
     * 根据名称、功效、类型获取药品
     * @param drug
     * @return
     */
    @Override
    public List<Drug> getDrugsByType(Drug drug) {
        List<Drug> drugList=drugMapper.getDrugsByType(drug.getEfficacy(),drug.getType());
        return drugList;
    }
}
